package com.rs.lc.pm.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by Xavier_Wei on 16/2/29.
 */
public class RandomUtils {
    /**
     * 盛昌随即字符串
     *
     * @param strCount 字符串的长度
     * @param strTable 字符串的字符来自的空间集
     * @return
     */
    public static String createRandomCode(int strCount, String strTable) {
        String retStr = "";
        if (StringUtils.isBlank(strTable)) {
            strTable = "1234567890abcdefghigklmnopqrstuvwxyz";
        }
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < strCount; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }

    /**
     * 生产一段随机字符串包含,字符来自"1234567890abcdefghigklmnopqrstuvwxyz"
     *
     * @param strCount
     * @return
     */
    public static String createRandomCode(int strCount) {
        return createRandomCode(strCount, null);
    }
}
